﻿Public Class CDetailFeel

#Region "自定义属性"
    Dim _ID As String
    Public Property ID() As String
        Set(ByVal value As String)
            _ID = value
        End Set
        Get
            Return _ID
        End Get
    End Property

    Dim _strName As String
    Public Property StrName1() As String
        Get
            Return _strName
        End Get
        Set(ByVal value As String)
            _strName = value
        End Set
    End Property
#End Region

#Region "加载数据"
    Public Sub LoadData(ByVal strId As String)
        Dim rs As ADODB.Recordset = Nothing
        Dim dbtype As Integer = 0
        dbtype = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings("dbtype"))
        If dbtype = 1 Then
            rs = ct.GetTable("select * from XSCFEEL WHERE DATE=#" + strId + "#    and USERNAME='" + Me.StrName1 + "'")
        ElseIf dbtype = 2 Then
            rs = ct.GetTable("select * from XSCFEEL WHERE DATE='" + strId + "'    and USERNAME='" + Me.StrName1 + "'")
        ElseIf dbtype = 3 Then
            rs = ct.GetTable("select * from XSCFEEL WHERE date([DATE])='" + DateTime.Parse(strId).ToString("yyyy-MM-dd") + "'      and USERNAME='" + Me.StrName1 + "'")
        End If
        If rs IsNot Nothing Then
            If rs.EOF = False Then
                Me.lstStart.Text = CMainModule.CmnToStr(rs.Fields("PlANT").Value)
                Me.lstEnd.Text = CMainModule.CmnToStr(rs.Fields("COMPLETEPLANT").Value)
                Me.lstFeel.Text = CMainModule.CmnToStr(rs.Fields("FEEL").Value)
                Me.txtPath.Text = CMainModule.CmnToStr(rs.Fields("FILENAME").Value)
                '############共通的部分
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
        End If
        If Me.txtPath.Text.Trim = String.Empty Then
            Me.btnOpen.Enabled = False
        Else
            Me.btnOpen.Enabled = True
        End If
        rs = Nothing
    End Sub
#End Region

#Region "窗体初始化事件"
    Private Sub CDetailFeel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData(Me.ID)
    End Sub
#End Region

#Region "打开附加按钮"
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Try

            Dim rs As ADODB.Recordset = Nothing
            Dim dbtype As Integer = 0
            Dim strSql As String = ""
            dbtype = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings("dbtype"))
            If dbtype = 1 Then
                strSql = "select FILENAME,[FILE] from XSCFEEL WHERE DATE=#" + Me.ID + "#   and USERNAME='" + Me.StrName1 + "'"
                'rs = ct.GetUpdateRs("select FILENAME,FILE from XSCFEEL WHERE DATE=#" + Me.strDate + "#   and USERNAME='" + Me.strName + "'")
            ElseIf dbtype = 2 Then
                strSql = "select FILENAME,[FILE] from XSCFEEL WHERE DATE='" + Me.ID + "'   and USERNAME='" + Me.StrName1 + "'"
                'rs = ct.GetUpdateRs("select FILENAME,FILE from XSCFEEL WHERE DATE='" + Me.strDate + "'   and USERNAME='" + Me.strName + "'")
            ElseIf dbtype = 3 Then
                strSql = "select FILENAME,[FILE] from XSCFEEL WHERE date([DATE])='" + DateTime.Parse(Me.ID).ToString("yyyy-MM-dd") + "'   and USERNAME='" + Me.StrName1 + "'"
            End If
            rs = ct.GetUpdateRs(strSql)
            Dim strFileName As String
            strFileName = CmnToStr(rs.Fields("FILENAME").Value)
            If strFileName <> String.Empty Then
                Dim strPath As String
                strPath = System.Environment.GetEnvironmentVariable("TEMP")
                strPath = strPath + "\" + strFileName
                If System.IO.File.Exists(strPath) Then
                    System.IO.File.Delete(strPath)
                End If
                Dim sr As New ADODB.Stream
                sr.Type = ADODB.StreamTypeEnum.adTypeBinary
                sr.Open()
                sr.Write(rs.Fields("FILE"))
                'sr.SaveToFile(strPath)
                'sr.Close()
                'System.Diagnostics.Process.Start(strPath)
                '共通过程,让用户选择直接打开，或保存
                If ShowMessageResult("是否直接打开文件？选否弹出保存对话框" + vbNewLine + "就是为了防止打开文件出错，如JS文件") = Windows.Forms.DialogResult.Yes Then
                    sr.SaveToFile(strPath)
                    System.Diagnostics.Process.Start(strPath)
                Else
                    Dim frmSaveDialog As Windows.Forms.SaveFileDialog = New Windows.Forms.SaveFileDialog()
                    frmSaveDialog.RestoreDirectory = True
                    frmSaveDialog.FileName = strFileName
                    frmSaveDialog.Filter = "(*" + System.IO.Path.GetExtension(strFileName) + ")|*" + System.IO.Path.GetExtension(strFileName)
                    If frmSaveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        If System.IO.File.Exists(frmSaveDialog.FileName) Then
                            System.IO.File.Delete(frmSaveDialog.FileName)
                        End If
                        sr.SaveToFile(frmSaveDialog.FileName)
                    End If
                End If
                sr.Close()
                sr = Nothing
                '共通过程,让用户选择直接打开，或保存
            End If
        Catch ex As Exception

        End Try
    End Sub
#End Region

#Region "更新记录 A-2009-08-25"
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'D-2009-08-29
        'Dim strSql As String = "select DISTINCT DATE from XSCFEEL WHERE date([DATE])='" + DateTime.Parse(ID).ToString("yyyy-MM-dd") + "'      and USERNAME='" + Me.StrName1 + "'"
        'A-2009-08-29
        Dim strSql As String = "select date([DATE]) AS DATE from XSCFEEL WHERE date([DATE])='" + DateTime.Parse(ID).ToString("yyyy-MM-dd") + "'      and USERNAME='" + Me.StrName1 + "'"
        Dim strSql1 As String = "select count(*) AS PageCount from XSCFEEL WHERE date([DATE])='" + DateTime.Parse(ID).ToString("yyyy-MM-dd") + "'      and USERNAME='" + Me.StrName1 + "'"
        Dim frm As New LISTFEEL
        frm.iType = Me.iType
        frm.strName = Me.strName
        frm.iListToUpdate = 0
        frm.LoadListToUpdate(strSql, strSql1)
        frm.ShowDialog()
        LoadData(ID)
    End Sub
#End Region

End Class
